/*
 * Copyright (c) 2015, FPX and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package com.fpx.xms.api.base.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * @author shiwei
 * @date 2015年6月24日
 */
public class JsonUtils {
	private static final ObjectMapper mapper = new ObjectMapper();

    static {
        // 反序列化时忽略掉多余的属性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 将对象转换成json格式字符串
     * @param object
     * @return
     * @throws JsonProcessingException
     */
    public static String toJson(Object object) throws JsonProcessingException {
        return mapper.writeValueAsString(object);
    }

    /**
     * 从json格式字符串转换成对象
     * @param json
     * @param clazz
     * @return
     * @throws IOException 
     * @throws JsonMappingException 
     * @throws JsonParseException 
     * @throws Exception
     */
    public static <T> T fromJson(String json, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
        return (T) mapper.readValue(json, clazz);
    }
    
    /**
     * 从json格式字符串转换成对象
     * @param json
     * @param clazz
     * @return
     * @throws IOException 
     * @throws JsonMappingException 
     * @throws JsonParseException 
     * @throws Exception
     */
    public static <T> T fromJson(String json, TypeReference<T> type) throws JsonParseException, JsonMappingException, IOException {
        return mapper.readValue(json, type);
    }
    
    /**
     * 将json转换成list对象
     * @param json
     * @param clazz
     * @return
     * @throws IOException 
     * @throws JsonMappingException 
     * @throws JsonParseException 
     */
    public static <T> List<T> fromJsonList(String json, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
        JavaType t = mapper.getTypeFactory().constructParametricType(List.class, clazz);
        return mapper.readValue(json, t);
    }

    /**
     * 将json转换成Map对象
     * @param json
     * @param clazzk Map中key的类型
     * @param clazzv Map中value的类型
     * @return
     * @throws IOException 
     * @throws JsonMappingException 
     * @throws JsonParseException 
     */
    public static <K, V> Map<K, V> fromJsonMap(String json, Class<K> clazzk, Class<V> clazzv) throws JsonParseException, JsonMappingException, IOException {
        JavaType t = mapper.getTypeFactory().constructParametricType(Map.class, clazzk, clazzv);
        return mapper.readValue(json, t);
    }
}
